package com.platform.api.bzproject.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.api.bzproject.model.BzAllWallpaper;
import com.platform.api.bzproject.service.BzAllWallpaperService;
import com.platform.api.common.service.ByteService;
import com.platform.common.core.Result;
import com.platform.common.core.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author guoyunfei on 2021/12/3 15:11
 * @className BzAllWallpaperJob
 * @description TODO  壁纸审核
 * @Version 1.0
 */

@Slf4j
@Component
public class BzAllWallpaperJob {

    @Autowired
    private BzAllWallpaperService bzAllWallpaperService;

    @Autowired
    private ByteService byteService;

    @Value("${spring.profiles.active}")
    private String active;

    @Async
    //@Scheduled(cron = "0/5 * * * * ?") //或直接指定时间间隔，例如：5秒
    @Scheduled(cron = "0 0/20 * * * ?") //15分钟执行一次
    @Transactional(rollbackFor = Exception.class)
    public void approveWallpaper() throws IOException, NoSuchAlgorithmException, KeyManagementException {
        System.out.println("执行了approveWallpaper");

        if("dev".equals(active)){
            return;
        }

        long starttime = System.currentTimeMillis();

        List<BzAllWallpaper> wallpapers = bzAllWallpaperService.list(
                new QueryWrapper<BzAllWallpaper>().eq("status", 1)
        .eq("is_sys",0).last("limit 100"));

        int size = wallpapers.size();

        if(size > 0){
            byteService.getAccessToken();

            //Collection<Integer> removeList = new ArrayList<>();
            Collection<BzAllWallpaper> removeList = new ArrayList<>();
            Collection<BzAllWallpaper> updateList = new ArrayList<>();

            try {
                //普通循环
                for(int i =0;i<size;i++){
                    BzAllWallpaper bzAllWallpaper = wallpapers.get(i);
                    //Result result = byteService.testingImage(bzAllWallpaper.getImage());
                    Result result = byteService.testingImage(bzAllWallpaper.getThumbnail());
                    if(result.getCode().intValue() != 200){
                        //判断是什么错误   1-参数有误   2-access_token 验证失败  3-图片下载失败   4-服务器内部错误
//                        if(!"1".equals(result.getData()) && !"2".equals(result.getData()) && !"3".equals(result.getData()) && !"4".equals(result.getData())){
//                            removeList.add(bzAllWallpaper.getId());
//                        }

                        if(result.getCode().intValue() == 10008){
                            //removeList.add(bzAllWallpaper.getId());
                            bzAllWallpaper.setStatus(3);
                            removeList.add(bzAllWallpaper);
                        }
                    }else{
                        BzAllWallpaper u = new BzAllWallpaper();
                        u.setId(bzAllWallpaper.getId());
                        u.setStatus(2);
                        updateList.add(u);
                    }
                }
            }catch (Exception e){
                log.info("定时任务审核异常  ==== {}",e.getMessage());
            }

            if(removeList.size() > 0){
                //删除不合理数据
                //bzAllWallpaperService.removeByIds(removeList);
                bzAllWallpaperService.updateBatchById(removeList);
            }
            if(updateList.size() > 0){
                //修改数据审核通过
                bzAllWallpaperService.updateBatchById(updateList);
            }

        }

        long endtime = System.currentTimeMillis();
        System.out.println("完成任务一消耗的时间"+(endtime-starttime)+"毫秒");
    }

}
